package com.bahehe.hsinchubus;

import java.util.ArrayList;



import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CustomBaseAdapter1 extends BaseAdapter implements AdapterView.OnItemLongClickListener {

	private static ArrayList<SearchResultsFavor> searchArrayList;
	private LayoutInflater mInflater;
	private Context mContext;

	int selected = -1;
	public CustomBaseAdapter1(Context context, ArrayList<SearchResultsFavor> results) {
		  searchArrayList = results;
		  mInflater = LayoutInflater.from(context);
		  mContext = context;
	}
    
	

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return  searchArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return searchArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	 static class ViewHolder {
		  TextView txtBusStop;
		  TextView txtRouteName;
		  TextView txtBusTime;
		  
	 }
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		
		 if (convertView==null) {
			   convertView = mInflater.inflate(R.layout.list_myfavor, null);
			   holder = new ViewHolder();
			   
			   holder.txtBusStop = (TextView) convertView.findViewById(R.id.txtBusStop);
			   holder.txtRouteName = (TextView) convertView.findViewById(R.id.txtBusRoute);
			   holder.txtBusTime = (TextView) convertView.findViewById(R.id.txtBusTime);
			  
			   convertView.setTag(holder);
			  } else {
			   holder = (ViewHolder) convertView.getTag();
			  }
			  
		 	  holder.txtBusStop.setText(searchArrayList.get(position).getBusstop());
		 	  holder.txtRouteName.setText(searchArrayList.get(position).getRoute_name());
		 	  holder.txtBusTime.setText(searchArrayList.get(position).getBustime());

		 	  
		 	 if(position == mItemIndex){
		 		convertView.setSelected(true);
		 		convertView.setPressed(true);
		 		convertView.setBackgroundColor(Color.argb(150, 22, 70, 150));
		 	 }else {
		 		convertView.setSelected(false);
		 		convertView.setPressed(false);
		 		convertView.setBackgroundColor(Color.rgb(250, 250, 250));
			}
		
	 	 return convertView;
	}

	private int mItemIndex = -1;

	public void setSelectItem(int position) {
		// TODO Auto-generated method stub
		mItemIndex = position;
	}



//	@Override
//	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//		
//		Log.i("MyFavor","onItemClick = " +arg2 );
//		
////		if(searchArrayList!=null && searchArrayList.size()>0)
////		 new AlertDialog.Builder(mContext).setTitle(searchArrayList.get(arg2-1).getRoute_name())
////		 .setMessage(searchArrayList.get(arg2-1).getBusstop()).show();
//		   
//		// TODO Auto-generated method stub
//		if (arg2 != this.selected)
//	      {
//	        this.selected = arg2;
//	        notifyDataSetChanged();
//	      }
//		
//		
//	}



	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
	Log.i("MyFavor","onItemClick = " +arg2 );
	Log.i("MyFavor","selected = " +this.selected );
		
//		if(searchArrayList!=null && searchArrayList.size()>0)
//		 new AlertDialog.Builder(mContext).setTitle(searchArrayList.get(arg2-1).getRoute_name())
//		 .setMessage(searchArrayList.get(arg2-1).getBusstop()).show();
		   
		// TODO Auto-generated method stub
		if (arg2 != this.selected)
	      {
	        this.selected = arg2;
	        notifyDataSetChanged();
	      }
		return false;
	}

}
